#pragma once
#include <Windows.h>
#include <string>
#include <DbgHelp.h>

#pragma comment(lib,"Dbghelp.lib")

class CPeFile
{
    std::string m_strFile;
    DWORD m_dwFileSize;
    HANDLE m_hFile;
    HANDLE m_hFileMap;
    LPVOID m_lpPeBuff;
    PIMAGE_NT_HEADERS32 m_lpNtHeaders32;
    PIMAGE_NT_HEADERS64 m_lpNtHeaders64;
    PIMAGE_OPTIONAL_HEADER32 m_lpOptHeader32;
    PIMAGE_OPTIONAL_HEADER64 m_lpOptHeader64;
    PIMAGE_SECTION_HEADER m_lpFirstSection;

public:
    CPeFile();
    CPeFile(IN PCSTR lpszFile);
    ~CPeFile();

private:
    void Initialize(IN UINT32 Phase);
    void ClearRs();

public:
    BOOL OpenFile(IN PCSTR lpszFile);
    BOOL IsArch64();
    BOOL IsDLL();
    BOOL IsExecutable();
    BOOL IsSystem();
    DWORD RvaToFoa(IN DWORD dwRva);
    PVOID RvaToVa(IN DWORD dwRva);
    WORD GetNumberOfSections();
    const PIMAGE_DATA_DIRECTORY GetDataDirectory(IN unsigned int nIndex);
    const LPVOID GetPeBuff();
    const DWORD GetPeFileSize();
    ULONG_PTR GetImageBase();
    DWORD GetImageSize();
    DWORD GetAddressOfEntryPoint();
    DWORD GetPeHeaderSize();
    DWORD GetOffsetToFirstSection();
    DWORD GetSectionSize();
    PIMAGE_SECTION_HEADER FindSectionByName(PCSTR lpName);
};
